﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using EZCRS.HCMS.Entity;
using EZCRS.HCMS.Bll;

namespace EZCRS.HCMS.Web.manage.hotel
{
    public partial class seasonlist : ManageBase
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.BasHotelId == 0)
                Response.Write(CFun.AlertMeg(Resources.Site.MustHotel, "back", ""));

            if (!Page.IsPostBack)
            {
                Titlepage1.initlabnew = "<a href=\"seasonlist_add.aspx\"><img src=\"../../image/new.gif\" border=\"0\"></a> ";
            }

            if (!IsPostBack)
            {
                CheckModule(67);

                if (Request.QueryString["action"] != null)
                {
                    //get data
                    if (Request.QueryString["action"].ToLower() == "get")
                    {
                        int iPageIndex = 1;
                        int iPageSize = 15;

                        int iState = 1;
                        string strRoom = string.Empty;

                        if (!string.IsNullOrEmpty(Request.Params["page"]))
                            int.TryParse(Request.Params["page"], out iPageIndex);

                        if (!string.IsNullOrEmpty(Request.Params["rp"]))
                            int.TryParse(Request.Params["rp"], out iPageSize);

                        Response.Write(GetJSON(iPageIndex, iState, strRoom, iPageSize));
                        Response.End();
                    }
                    else if(Request.QueryString["action"].ToLower() == "del")
                    {
                        int iID = 0;

                        if (!string.IsNullOrEmpty(Request.Params["id"]))
                            int.TryParse(Request.Params["id"], out iID);

                        bool b = DoDelete(iID);

                        if(b)
                            Response.Write("{state:1}");
                        else
                            Response.Write("{state:0}");

                        Response.End();
                    }
                }
            }
        }

        private Dictionary<string, string> WeekDictionary()
        {
            Dictionary<string, string> dic = new Dictionary<string, string>();

            dic.Add("0", Resources.Site.LabMonday);
            dic.Add("1", Resources.Site.LabTuesday);
            dic.Add("2", Resources.Site.LabWednesday);
            dic.Add("3", Resources.Site.LabThursday);
            dic.Add("4", Resources.Site.LabFriday);
            dic.Add("5", Resources.Site.LabSaturday);
            dic.Add("6", Resources.Site.LabSunday);

            dic.Add("m1", Resources.Site.LabMonday);
            dic.Add("m2", Resources.Site.LabTuesday);
            dic.Add("m3", Resources.Site.LabWednesday);
            dic.Add("m4", Resources.Site.LabThursday);
            dic.Add("m5", Resources.Site.LabFriday);
            dic.Add("m6", Resources.Site.LabSaturday);
            dic.Add("m7", Resources.Site.LabSunday);

            return dic;
        }

        private bool DoDelete(int ID)
        {
            BllRatePlan bllPlan = new BllRatePlan();

            if (ID > 0)
                return bllPlan.DeleteSeasonPlan(ID);

            return true;
        }

        private string GetJSON(int PageIndex, int State, string Room, int PageSize)
        {
            BllRatePlan bllPlan = new BllRatePlan();

            int iRowCount = 0;


            List<HCRS_Season> lst =
                bllPlan.getSeasonList(base.BasPropertyId, PageIndex, PageSize, out iRowCount);

            string strJson = "{\"total\":" + iRowCount + ",\"page\":" + PageIndex.ToString() + ",\"rows\":[";

            Dictionary<string, string> dic = WeekDictionary();


            foreach (HCRS_Season obj in lst)
            {
                strJson += "{\"id\":" + obj.ID.ToString() + ",";
                strJson += "\"cell\":[";

                strJson += "\"" + HttpUtility.HtmlEncode(obj.Name) + "\",";
                strJson += "\"" + obj.StartDate.ToString("yyyy-MM-dd") + "\",";
                strJson += "\"" + obj.EndDate.ToString("yyyy-MM-dd") + "\",";

                string [] week = obj.Week.Split(',');
                string strWeek = "";

                foreach (string s in week)
                {
                    if (dic.ContainsKey(s))
                        strWeek += dic[s] + ",";
                }

                strJson += "\"" + strWeek.TrimEnd(',') + "\",";

                strJson += "]},";
            }

            strJson = strJson.Replace("\r\n", " ");
            strJson = strJson.TrimEnd(',');
            strJson += "]}";

            return strJson;
        }

        
    }
}
